// 实时监测  赵

var express = require('express');
var router = express.Router();
const db = require('../../db/db');


// 视屏监控

// 获取牧场下拉列表信息
router.get('/getMchInfo', function (req, res) { 
  const sql = `select * from nc_pasture`;
  db.query(sql, (err, result) => { 
    if (err) return res.cc(0,err);
    res.cc(200, '获取信息成功', result);
   })

});

// 获取设备列表信息
router.get('/getVideoInfo', function (req, res) {
  // monitor = 1: 监控设备 
  // pasture = 1: 牧场 
  const { pageSize, pageNum, F_Id, pasture,monitor, } = req.query;

  // 参数处理
  const parsedPageSize = parseInt(pageSize) || 5;
  const parsedPageNum = parseInt(pageNum) || 1;
  const offset = (parsedPageNum - 1) * parsedPageSize;

  // 初始化参数数组
  const params = [];
  let whereClause = 'WHERE 1=1'; 

  // 动态条件拼接
  if (F_Id) {
    whereClause += ' AND F_Org_Id = ?';
    params.push(F_Id);
  }
  // 改为模糊查询（新增 % 通配符）
if (monitor) {
  whereClause += ' AND F_Title LIKE ?';  // 模糊匹配字段
  params.push(`%${monitor}%`);          // 参数值添加通配符
}
if (pasture) {
  whereClause += ' AND F_Org_Id IN (SELECT F_Id FROM nc_pasture WHERE F_Title LIKE ?)';
  params.push(`%${pasture}%`);          // 子查询也改为模糊匹配
}

  // 完整 SQL（参数化分页）
  const sql = `SELECT * FROM nc_device_video 
               ${whereClause}
               LIMIT ?, ?`;
  params.push(offset, parsedPageSize);

  db.query(sql, params, (err, result) => {
    if (err) return res.cc(0, err);
    res.cc(200, '获取信息成功', result);
  });
});

// 获取养殖舍下拉列表
router.get('/getPastureInfo', function (req, res) {
  const sql = `select F_Id ,F_Title from nc_house`;
  db.query(sql, (err, result) => { 
    if (err) return res.cc(0,err);
    res.cc(200, '获取信息成功', result);
   })
});

// 围栏监控
router.get('/getFenceInfo', function (req, res) {
  const { title } = req.query;
  let sql, params = [];
  if (!title) {
    sql = `
      select * from nc_fence
    `;
  } else {
    sql = `
       SELECT f.*
      FROM nc_pasture p
      INNER JOIN nc_fence f ON p.F_Id = f.F_Org_Id
      WHERE p.F_Title = ?
    `;
    params.push(title);
  }
  db.query(sql, params, (err, result) => { 
    if (err) return res.cc(0,err);
    res.cc(200, '获取信息成功', result);
  });
});

// 清空电子围栏
router.get('/clearFence', function (req, res) {
  const { F_Org_Id } = req.query;
  let sql, params = [];
  // 删除F_Map

    sql = `
      UPDATE nc_fence SET F_Map = NULL WHERE F_Org_Id = ?;
    `;
    params.push(F_Org_Id);
  db.query(sql, params, (err, result) => { 
    if (err) return res.cc(0,err);
    res.cc(200, '清空成功', result);
  })
});

// 保存电子围栏信息
router.post('/saveFence', function (req, res) {
  const { F_Org_Id, F_Map } = req.body;
  sql = `
    UPDATE nc_fence SET F_Map = ? WHERE F_Org_Id = ?;
  `;
  db.query(sql, [F_Map, F_Org_Id] , (err, result) => { 
    if (err) return res.cc(0,err);
    res.cc(200, '保存成功', result);
  })
});



// 环境监控
router.get('/getEnvInfo', function (req, res) {
  const { title } = req.query;
  let sql, params = [];

  // 无title时的查询逻辑
  if (!title) {
    sql = `
      select * from nc_setting
    `;
  } 
  // 有title时的多级联查
  else {
    sql = `
     SELECT 
    p.F_Title,    -- 牧场名称置于最前
    s.*           -- 获取nc_setting表全部字段
    FROM nc_pasture p
  INNER JOIN nc_setting s ON p.F_Id = s.F_Org_Id  -- 通过F_Id与F_Org_Id关联
   WHERE p.F_Title = ?   -- 参数化查询条件
    `;
    params.push(title);
  }

  // 执行查询（参数化处理）
  db.query(sql, params, (err, result) => { 
    if (err) return res.cc(0,err);
    res.cc(200, '获取信息成功', result);
  });
});

// 获取健康情况
router.get('/getHealthInfo', function (req, res) { 
  const { title, house } = req.query;
  let sql, params = [];

  // 条件分支处理
  if (!title && !house) {
    // 无参数时的原始查询（您已实现）
     sql = `
    SELECT 
    h.F_Title,
    g.liveid, g.pjtw, g.zdtw, g.zgtw, g.twzt, 
    g.zl, g.tzzb, g.jrbs, g.xqzt, g.rq, g.orgid
FROM nc_house h
INNER JOIN nc_growinfo g ON h.F_Org_Id = g.orgid 
    `
  } else if (title && house) {
    sql = `
      SELECT h.F_Title, g.liveid, g.pjtw, g.zdtw, g.zgtw, g.twzt, 
             g.zl, g.tzzb, g.jrbs, g.xqzt, g.rq, g.orgid
      FROM nc_pasture p
      INNER JOIN nc_house h ON p.F_Id = h.F_Org_Id
      INNER JOIN nc_growinfo g ON h.F_Org_Id = g.orgid
      WHERE p.F_Title = ? AND h.F_Title = ?
    `;
    params.push(title, house);
  } else if (title) {
    sql = `
      SELECT h.F_Title, g.liveid, g.pjtw, g.zdtw, g.zgtw, g.twzt, 
             g.zl, g.tzzb, g.jrbs, g.xqzt, g.rq, g.orgid
      FROM nc_pasture p
      INNER JOIN nc_house h ON p.F_Id = h.F_Org_Id
      INNER JOIN nc_growinfo g ON h.F_Org_Id = g.orgid
      WHERE p.F_Title = ?
    `;
    params.push(title);
  } else if (house) {
    sql = `
      SELECT h.F_Title, g.liveid, g.pjtw, g.zdtw, g.zgtw, g.twzt, 
             g.zl, g.tzzb, g.jrbs, g.xqzt, g.rq, g.orgid
      FROM nc_house h
      INNER JOIN nc_growinfo g ON h.F_Org_Id = g.orgid
      WHERE h.F_Title = ?
    `;
    params.push(house);
  }

  db.query(sql, params, (err, result) => { 
    if (err) return res.cc(0,err);
    res.cc(200, '获取信息成功', result);
  });
});






module.exports = router;